package com.llkj.safeearth.keepalive;

import android.content.Context;
import com.llkj.safeearth.SafeEarthApp;

/**
 * 新增保活管理类
 * Created by Administrator on 2017/11/22.
 */

public class KeepingAliveManager {
    private static KeepingAliveManager aliveManager;
    // 动态注册锁屏等广播
    private ScreenReceiverUtil mScreenListener;
    // 1像素Activity管理类
    private ScreenManager mScreenManager;
    // JobService，执行系统任务
    private JobSchedulerManager mJobManager;

    public static KeepingAliveManager getAliveManagerIntance(){
        if(aliveManager==null){
            aliveManager = new KeepingAliveManager();
        }
        return aliveManager;
    }

    /**
     * 注册锁屏广播监听
     */
    public void registerLockScreenBrocastReciever(){
        Context context = SafeEarthApp.getInstance().getApplicationContext();
        // 1. 注册锁屏广播监听器
        mScreenListener = new ScreenReceiverUtil(context);
        mScreenManager = ScreenManager.getScreenManagerInstance(context);
        mScreenListener.setScreenReceiverListener(mScreenListenerer);

        // 2. 启动系统任务
        mJobManager = JobSchedulerManager.getJobSchedulerInstance(context);
        mJobManager.startJobScheduler();
    }

    private ScreenReceiverUtil.SreenStateListener mScreenListenerer = new ScreenReceiverUtil.SreenStateListener() {
        @Override
        public void onSreenOn() {
            // 亮屏，移除"1像素"
            mScreenManager.finishActivity();
        }

        @Override
        public void onSreenOff() {
            // 接到锁屏广播，将SportsActivity切换到可见模式
            // "咕咚"、"乐动力"、"悦动圈"就是这么做滴
//            Intent intent = new Intent(SportsActivity.this,SportsActivity.class);
//            startActivity(intent);
            // 如果你觉得，直接跳出SportActivity很不爽
            // 那么，我们就制造个"1像素"惨案
            mScreenManager.startActivity();
        }

        @Override
        public void onUserPresent() {
            // 解锁，暂不用，保留
        }
    };
}
